home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlib43 / mntlib / putenv.c < prev    next >
C/C++ Source or Header  |  1993-09-15  |  1KB  |  76 lines

  1. /* functions for manipulating the environment */
  2. /* written by Eric R. Smith and placed in the public domain */
  3.  
  4. #include <stddef.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #ifndef _COMPILER_H
  8. #include <compiler.h>
  9. #endif
  10. #include <support.h>
  11.  
  12. extern char ** environ;
  13.  
  14. static void del_env __PROTO((const char *strng));
  15.  
  16. static void
  17. del_env(strng)
  18.     const char *strng;
  19. {
  20.     char **var;
  21.     char *name;
  22.     size_t len = 0;
  23.  
  24.     if (!environ) return;
  25.  
  26. /* find the length of "tag" in "tag=value" */
  27.     for (name = (char *)strng; *name && (*name != '='); name++)
  28.         len++;
  29.  
  30. /* find the tag in the environment */
  31.     for (var = environ; (name = *var) != NULL; var++) {
  32.         if (!strncmp(name, strng, len) && name[len] == '=')
  33.             break;
  34.     }
  35.  
  36. /* if it's found, move all the other environment variables down by 1 to
  37.    delete it
  38.  */
  39.     if (name) {
  40.         while (name) {
  41.             name = var[1];
  42.             *var++ = name;
  43.         }
  44.     }
  45. }
  46.  
  47. int
  48. putenv(strng)
  49.     const char *strng;
  50. {
  51.     int i = 0;
  52.     char **e;
  53.  
  54.     del_env(strng);
  55.  
  56.     if (!environ)
  57.         e = (char **) malloc(2*sizeof(char *));
  58.     else {
  59.         while(environ[i]) i++ ;
  60.         e = (char **) malloc((i+2)*sizeof(char *));
  61.         if (!e) {
  62.             return -1;
  63.         }
  64.         bcopy(environ, e, (i+1)*sizeof(char *));
  65.         free(environ);
  66.         environ = e;
  67.     }
  68.     if (!e)
  69.         return -1;
  70.  
  71.     environ = e;
  72.     environ[i] = (char *)strng;
  73.     environ[i+1] = 0;
  74.     return 0;
  75. }
  76.